﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Mocola.BLL;
using Mocola.Model;
using Mocola.Web;
using Mocola.Security;
using Mocola.Common;
using System.Web.Security;
using System.Data.Common;

namespace Mocola.Net.Web
{
    public partial class departmentals : System.Web.UI.Page
    {
        protected string IsPasswordConfirm;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            IsPasswordConfirm = ConfigurationManager.AppSettings["PasswordConfirm"];
            string sql = "SELECT * FROM Moc_Departments";
            DataSet dtDept = Databases.MocolaNet.ExecuteDataSet(CommandType.Text, sql);
            rptDept.DataSource = dtDept;
            rptDept.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (DeptName.Value.Trim() == "")
            {
                Alert.Show("请输入部门名称！");
                DeptName.Focus();
                return;
            }
            MembershipCreateStatus status = new MembershipCreateStatus();
            DbTransaction transaction = Databases.MocolaNet.BeginTransaction(IsolationLevel.ReadCommitted);

            try
            {
                int nID = Databases.MocolaNet.Insert(Moc_DepartmentsInfo.Moc_Departments)
                            .AddColumn(Moc_DepartmentsInfo.Moc_Departments.Name, DeptName.Value.Trim())
                            .SetTransaction(transaction)
                            .ExecuteReturnAutoIncrementID(Moc_DepartmentsInfo.Moc_Departments.ID);

                bool blTrees = false;

                if (nID != -1)
                {
                    blTrees = true;
                }

                if (blTrees)
                {
                    Alert.Show("保存成功!");
                    status = MembershipCreateStatus.Success;
                    Response.Write("<script>location.replace('departmentals.aspx');</script>");
                }
                else
                {
                    Alert.Show("保存失败!");
                    status = MembershipCreateStatus.ProviderError;
                }
            }
            catch (Exception ex)
            {
                Alert.Show(ex.Message);
                status = MembershipCreateStatus.ProviderError;
            }
            finally
            {
                if (status == MembershipCreateStatus.Success)
                    transaction.Commit();
                else
                    transaction.Rollback();
                Databases.MocolaNet.CloseConnection(transaction);
            }
        }
    }
}
